using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace XnaDevRu.BulletX.Dynamics
{
	public class RaycastVehicle : TypedConstraint
	{
		public override void BuildJacobian()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override void SolveConstraint(float timeStep)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public int getNumWheels()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public WheelInfo getWheelInfo(int v)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public void updateWheelTransform(int v, bool p)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public int getRightAxis()
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public void updateVehicle(float timeStep)
		{
			throw new Exception("The method or operation is not implemented.");
		}
	}

	public class DefaultVehicleRaycaster : IVehicleRaycaster
	{
		DynamicsWorld _dynamicsWorld;

		public DefaultVehicleRaycaster(DynamicsWorld world)
		{
			_dynamicsWorld = world;
		}

		public object CastRay(Vector3 from, Vector3 to, out VehicleRaycasterResult result)
		{
			CollisionWorld.ClosestRayResultCallback rayCallback = new CollisionWorld.ClosestRayResultCallback(from, to);
			_dynamicsWorld.RayTest(from, to, rayCallback);

			result = new VehicleRaycasterResult();

			if (!rayCallback.HasHit) return 0;
			RigidBody body = RigidBody.Upcast(rayCallback.CollisionObject);
			if (body == null) return 0;

			result.HitPointInWorld = rayCallback.HitPointWorld;
			result.HitNormalInWorld = rayCallback.HitNormalWorld;
			result.HitNormalInWorld.Normalize();
			result.DistFraction = rayCallback.ClosestHitFraction;
			return body;
		}
	}
}